package com.test.webmanage.controller;

import com.test.webmanage.pojo.Emp;
import com.test.webmanage.pojo.PageBean;
import com.test.webmanage.pojo.Result;
import com.test.webmanage.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;

@RestController
public class EmpController {
    @Autowired
    private EmpService empService;
    @GetMapping("/emps")
    public Result list(@RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer pageSize,
                       String name, Short gender,
                       @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
                       @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end) {
//        if(page==null)
//            page=1;
//        if(pageSize==null)
//            pageSize=10;
       PageBean pageBean = empService.list(page,pageSize,name,gender,begin,end);
        return  Result.success(pageBean);
    }
    @DeleteMapping("/emps/{ids}")
    public Result delete(@PathVariable List<Integer> ids) {
        empService.delete(ids);
        return Result.success();
    }
    @PostMapping("/emps")
    public Result add(@RequestBody Emp emp) {
        empService.add(emp);
        return Result.success();
    }
    @GetMapping("/emps/{id}")
    public Result getById(@PathVariable Integer id) {
        Emp emp = empService.getById(id);
        return Result.success(emp);
    }
    @PutMapping("/emps")
    public Result update(@RequestBody Emp emp) {
        empService.update(emp);
        return Result.success();
    }
}
